use std::io;

use actix_web::{App, error, get, HttpServer};

#[derive(Debug)]
struct MyError {
    name: &'static str,
}

#[get("/")]
async fn index() -> actix_web::Result<String> {
    let result = Err(MyError { name: "test error" });
    // 使用actix_web::error可以将未实现ResponseError的Error转换为特定的错误代码
    result.map_err(|err| error::ErrorBadRequest(err.name))
}

#[actix_web::main]
async fn main() -> io::Result<()> {
    HttpServer::new(|| App::new().service(index))
        .bind(("127.0.0.1", 8080))?
        .run()
        .await
}
